use strict;
use Tk;
use Tk::JComboBox;

my $mw = MainWindow->new(-height => 100, -width => 200);



# 
# % will be used to match everything from SQL database.
# The whole array will be '%'+ a initial SQL query,
# for example, List_A = '%'+ initial SQL query result.
my @List_A = (qw/% A B C D E F G/);
my @List_B = (qw/% 1 2 3 4 5 6 7 8 9 10/);


my $ListA_cb = $mw->JComboBox(
	-mode => 'readonly',
	-choices => \@List_A,
	# Everytime user made any selection, the updateComboBoxList will be trigger
	# there, all arrays List_A and List_B will be updated according 
	# user selection
	-selectcommand => \&updateComboBoxList
)->pack;

my $ListB_cb = $mw->JComboBox(
	-mode => 'readonly',
	-choices => \@List_B,
	# Everytime user made any selection, the updateComboBoxList will be trigger
	# there, all arrays List_A and List_B will be updated according 
	# user selection
	-selectcommand => \&updateComboBoxList
)->pack;

$mw->Button(
 	-text => "Exit",
	-command => sub { exit; }
)->pack;


# Make the first element as selected, '%'. means matching everying in SQL query script.
$ListA_cb->setSelectedIndex(0);
$ListB_cb->setSelectedIndex(0);


MainLoop();

sub updateComboBoxList
{
	# One user made selection, all arrays need to be updated.
	# for example List_A, contains '% A B C D E F G'.
	# User made selection 'G'. Then this array will be refreshed
	# It will only contains '% G' instead.
	# The '%' will give user option that he could remove this condition.
	
	# 
	my ($jcb, $selectedIndex, $selectedValue, $selectedName) = @_;

	print "\$selectedIndex:	$selectedIndex	\n";
	print "\$selectedValue:	$selectedValue	\n";
	
	
	
	#my @tmp;
	# 
	
	
	my $currSelectionListA = $ListA_cb->getSelectedValue();
	my $currSelectionListB = $ListB_cb->getSelectedValue();
	print "\$currSelectionListA:	$currSelectionListA \n";
	print "\$currSelectionListB:	$currSelectionListB \n";
	
	
	my @tmpA = (qw/% A1 B1 C1/);
	my @tmpB = (qw/% 5/);
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	#if ($selectedValue eq '%') {
		# if user selected '%', that means he  
		# need to a query to get the whole list from SQL based on user selections
		# from the two combobox.
		# let's make up one
	#	@tmp = (qw/% A1 B1 C1 /);
	#	@List_A = @tmp;
	
	#	return;
	#}

	# Make up a temporary array to hold the new choices.
	#@tmp = (qw /%/);
	# push the user selection to that array.
	#push @tmp, $selectedValue;
	# update the List_A array.
	
	#@List_A = @tmp;








	#foreach my $idx ( 0 .. $#List_A-1){
	#	print "\$selectedValue: $selectedValue 	\n";
	#	print "\$List_A[$idx]:	$List_A[$idx]	\n";
	#	next if ($selectedValue eq "%" || $List_A[$idx] eq $selectedValue);
	#	
	#	print "removing value: $List_A[$idx] \n";
	#	$jcb->removeItemAt($idx);
	#}
	
	
	
	
	
}

